#pragma once

#include <windows.h>

typedef void(*TaskCallbackFunction)(void* instance);

#define TASK_SLEEP_TIME_NULL -1

typedef enum
{
	TASK_PRIORITY_CRITICAL,
	TASK_PRIORITY_HIGH,
	TASK_PRIORITY_ABOVE_NORMAL,
	TASK_PRIORITY_NORMAL,
	TASK_PRIORITY_BELOW_NORMAL,
	TASK_PRIORITY_LOW,
	TASK_PRIORITY_IDLE
}T_TaskPriority;


/// \brief Task implemented with a thread
/// \details it receives the period and the function to execute
class Task
{
protected:

	HANDLE evento_sinc;
	HANDLE evento_fin;
	
	int sleep_time;

	bool end_thread;
	
	bool is_running;

	T_TaskPriority priority;

	TaskCallbackFunction cycle_function;

	void* owner;

	void sleep();

	void notify_end();

	void wait_for_real_end();

	void init_thread();

	virtual ~Task();

public:
	/// \brief constructor
	/// \param[in] f funcion callback a llamar en cada ciclo
	/// \param[in] owner data to the function
	/// \param[in] _priority task priority
	Task(TaskCallbackFunction f, void* owner, T_TaskPriority _priority, int sleep_time);

	void start();

	void stop();

	bool isRunning();

	static unsigned int task_cycle(void*);

	void indicateTaskCallbackFunction(TaskCallbackFunction f, void* owner);


};
